<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">
<link href="style.css" type="text/css" rel="stylesheet">
<title>BOUND—Check Array Index Against Bounds </title></head>
<body>
<h1>BOUND—Check Array Index Against Bounds</h1>
<table>
<tr>
<th>Opcode</th>
<th>Instruction</th>
<th>Op/En</th>
<th>64-bit Mode</th>
<th>Compat/Leg Mode</th>
<th>Description</th></tr>
<tr>
<td>62 /<em>r</em></td>
<td>BOUND <em>r16, m16&amp;16</em></td>
<td>RM</td>
<td>Invalid</td>
<td>Valid</td>
<td>Check if <em>r16</em> (array index) is within bounds specified by <em>m16&amp;16.</em></td></tr>
<tr>
<td>62 /<em>r</em></td>
<td>BOUND <em>r32, m32&amp;32</em></td>
<td>RM</td>
<td>Invalid</td>
<td>Valid</td>
<td>Check if <em>r32</em> (array index) is within bounds specified by <em>m32&amp;32.</em></td></tr></table>
<h3>Instruction Operand Encoding</h3>
<table>
<tr>
<td>Op/En</td>
<td>Operand 1</td>
<td>Operand 2</td>
<td>Operand 3</td>
<td>Operand 4</td></tr>
<tr>
<td>RM</td>
<td>ModRM:reg (r)</td>
<td>ModRM:r/m (r)</td>
<td>NA</td>
<td>NA</td></tr></table>
<h2>Description</h2>
<p>BOUND determines if the first operand (array index) is within the bounds of an array specified the second operand (bounds operand). The array index is a signed integer located in a register. The bounds operand is a memory loca-tion that contains a pair of signed doubleword-integers (when the operand-size attribute is 32) or a pair of signed word-integers (when the operand-size attribute is 16). The first doubleword (or word) is the lower bound of the array and the second doubleword (or word) is the upper bound of the array. The array index must be greater than or equal to the lower bound and less than or equal to the upper bound plus the operand size in bytes. If the index is not within bounds, a BOUND range exceeded exception (#BR) is signaled. When this exception is generated, the saved return instruction pointer points to the BOUND instruction.</p>
<p>The bounds limit data structure (two words or doublewords containing the lower and upper limits of the array) is usually placed just before the array itself, making the limits addressable via a constant offset from the beginning of the array. Because the address of the array already will be present in a register, this practice avoids extra bus cycles to obtain the effective address of the array bounds.</p>
<p>This instruction executes as described in compatibility mode and legacy mode. It is not valid in 64-bit mode.</p>
<h2>Operation</h2>
<pre>IF 64bit Mode
    THEN
         #UD;
    ELSE
         IF (ArrayIndex &lt; LowerBound OR ArrayIndex &gt; UpperBound)
         (* Below lower bound or above upper bound *)
              THEN #BR; FI;
FI;</pre>
<h2>Flags Affected</h2>
<p>None.</p>
<h2>Protected Mode Exceptions</h2>
<table class="exception-table">
<tr>
<td>#BR</td>
<td>If the bounds test fails.</td></tr>
<tr>
<td>#UD</td>
<td>
<p>If second operand is not a memory location.</p>
<p>If the LOCK prefix is used.</p></td></tr>
<tr>
<td>#GP(0)</td>
<td>
<p>If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit.</p>
<p>If the DS, ES, FS, or GS register contains a NULL segment selector.</p></td></tr>
<tr>
<td>#SS(0)</td>
<td>If a memory operand effective address is outside the SS segment limit.</td></tr>
<tr>
<td>#PF(fault-code)</td>
<td>If a page fault occurs.</td></tr>
<tr>
<td>#AC(0)</td>
<td>If alignment checking is enabled and an unaligned memory reference is made while the current privilege level is 3.</td></tr></table>
<h2>Real-Address Mode Exceptions</h2>
<table class="exception-table">
<tr>
<td>#BR</td>
<td>If the bounds test fails.</td></tr>
<tr>
<td>#UD</td>
<td>
<p>If second operand is not a memory location.</p>
<p>If the LOCK prefix is used.</p></td></tr>
<tr>
<td>#GP</td>
<td>If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit.</td></tr>
<tr>
<td>#SS</td>
<td>If a memory operand effective address is outside the SS segment limit.</td></tr></table>
<h2>Virtual-8086 Mode Exceptions</h2>
<table class="exception-table">
<tr>
<td>#BR</td>
<td>If the bounds test fails.</td></tr>
<tr>
<td>#UD</td>
<td>
<p>If second operand is not a memory location.</p>
<p>If the LOCK prefix is used.</p></td></tr>
<tr>
<td>#GP(0)</td>
<td>If a memory operand effective address is outside the CS, DS, ES, FS, or GS segment limit.</td></tr>
<tr>
<td>#SS(0)</td>
<td>If a memory operand effective address is outside the SS segment limit.</td></tr>
<tr>
<td>#PF(fault-code)</td>
<td>If a page fault occurs.</td></tr>
<tr>
<td>#AC(0)</td>
<td>If alignment checking is enabled and an unaligned memory reference is made.</td></tr></table>
<h2>Compatibility Mode Exceptions</h2>
<p>Same exceptions as in protected mode.</p>
<h2>64-Bit Mode Exceptions</h2>
<table class="exception-table">
<tr>
<td>#UD</td>
<td>If in 64-bit mode.</td></tr></table></body></html>